<template>
    <el-dialog :title="title" :visible.sync="open" width="40%" :before-close="handleClose" class="dialogClass">
        <!-- 表单开始 -->
        <el-form ref="ruleForm" :model="ruleForm" label-width="100px">
            <el-row :gutter="20">
                <el-col>
                    <el-form-item label="当前资产价值" prop="dangqianzichan">
                        <el-input v-model="ruleForm.dangqianzichan" placeholder="请输入当前资产价值" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item label="牛舍" prop="niushe">
                        <el-input v-model="ruleForm.niushe" placeholder="请输入牛舍" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item label="牛群状态" prop="niuqunzhuangtai">
                        <el-input v-model="ruleForm.niuqunzhuangtai" placeholder="请输入牛群状态" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item label="繁殖状态" prop="fanzhizhuangtai">
                        <el-input v-model="ruleForm.fanzhizhuangtai" placeholder="请输入繁殖状态" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <!-- 表单结束 -->
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" size="big" @click="onSubmit('ruleForm')" :loading="loading">{{ submitButton
            }}</el-button>
            <el-button size="big" @click="resetForm('ruleForm')">重置</el-button>
            <el-button size="big" @click="close('ruleForm')">取消</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { updateAPI, saveAPI } from "@/apis/employee/bullAsset.js";
export default {
    data() {
        return {
            // 提交按钮
            submitButton: "",
            // 控制弹窗
            open: false,
            // 控制按钮加载状态
            loading: false,
            // 弹窗标题
            title: "",
            // 表单项
            ruleForm: {
                dangqianzichan: "",
                niushe: "",
                niuqunzhuangtai: "",
                fanzhizhuangtai: "",
            },
        };
    },
    methods: {
        //*查找货物类别
        queryCategory() {
            queryAllCategory()
                .then((res) => {
                    this.options = res.data;
                })
                .catch(() => { });
        },
        //* 查找单位
        searchUnit() {
            const _this = this;
            queryAllUnit().then(function (res) {
                _this.unit = res.data;
            });
        },
        //* 控制弹窗
        show(title, row) {
            const _this = this;
            _this.title = title;
            _this.open = true;
            if (row) {
                _this.submitButton = "修改";
                _this.ruleForm = JSON.parse(JSON.stringify(row));
            } else {
                _this.submitButton = "确定";
            }
        },
        close(formName) {
            this.open = false;
            this.resetForm(formName);
            this.tableData = [];
        },
        //*关闭弹窗
        handleClose(done) {
            const _this = this;
            _this
                .$confirm("系统可能不会保存您的更改", "确认关闭？")
                .then((_) => {
                    done();
                    _this.resetForm("ruleForm");
                })
                .catch((_) => { });
        },
        //*修改
        onSubmit(formName) {
            let _this = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (_this.title == "新增牛只资产卡") {
                        saveAPI(_this.ruleForm).then(function (res) {
                            if (res.code === 0) {
                                _this.$alert(_this.ruleForm.niushe + res.msg, "提示", {
                                    confirmButtonText: "确定",
                                    callback: (action) => {
                                        _this.close("ruleForm");
                                        _this.$parent.$parent.search();
                                    },
                                });
                            }
                        });
                    } else {
                        updateAPI(_this.ruleForm).then(function (res) {
                            if (res.code === 0) {
                                _this.$alert(_this.ruleForm.niushe + res.msg, "提示", {
                                    confirmButtonText: "确定",
                                    callback: (action) => {
                                        _this.close("ruleForm");
                                        _this.$parent.$parent.search();
                                    },
                                });
                            }
                        });
                    }
                }
            });
        },
        //* 表单重置
        resetForm(formName) {
            const _this = this;
            _this.$refs[formName].resetFields();
            _this.ruleForm.dangqianzichan = ''
            _this.ruleForm.niushe = ''
            _this.ruleForm.niuqunzhuangtai = ''
            _this.ruleForm.fanzhizhuangtai = ''
        },
    },
};
</script>

<style></style>